//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ZeroInflationIndex.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Indexes/Region.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Termstructures/ZeroInflationTermStructure.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/InflationIndex.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (String^ familyName, Cephei::QL::Indexes::IRegion^ region, Boolean revised, Boolean interpolated, QL::Times::FrequencyEnum frequency, Cephei::QL::Times::IPeriod^ availabilityLag, Cephei::QL::ICurrency^ currency, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>^ ts) : CInflationIndex(CZeroInflationIndex::typeid)
{
    CRegion^ _Cregion;
    CPeriod^ _CavailabilityLag;
    CCurrency^ _Ccurrency;
    CZeroInflationTermStructure^ _Cts;
    try
    {
#ifdef HANDLE
        _phZeroInflationIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName); //d
        _Cregion = safe_cast<CRegion^> (region);
        _Cregion->Lock();
        QuantLib::Region& _region = static_cast<QuantLib::Region&> (_Cregion->GetReference ()); 
        bool _revised = (bool)ValueHelper::Convert (revised); //d
        bool _interpolated = (bool)ValueHelper::Convert (interpolated); //d
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        _CavailabilityLag = safe_cast<CPeriod^> (availabilityLag);
        _CavailabilityLag->Lock();
        QuantLib::Period& _availabilityLag = static_cast<QuantLib::Period&> (_CavailabilityLag->GetReference ()); 
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency& _currency = static_cast<QuantLib::Currency&> (_Ccurrency->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>::IsSome::get (ts))
        {
            _Cts = safe_cast<CZeroInflationTermStructure^> (ts->Value);
            _Cts->Lock();
        }
        Handle<QuantLib::ZeroInflationTermStructure>& _ts = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>::IsSome::get (ts) ? static_cast<Handle<QuantLib::ZeroInflationTermStructure>&> (_Cts->GetHandle ()) : Handle<QuantLib::ZeroInflationTermStructure>()); //1
        _ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (new QuantLib::ZeroInflationIndex ( _familyName,  _region,  _revised,  _interpolated,  _frequency,  _availabilityLag,  _currency,  _ts ));
        SetInflationIndex (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cregion != nullptr) _Cregion->Unlock();
        if (_CavailabilityLag != nullptr) _CavailabilityLag->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_Cts != nullptr) _Cts->Unlock();
    }
}
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (boost::shared_ptr<QuantLib::ZeroInflationIndex>& childNative, Object^ owner) : CInflationIndex(CZeroInflationIndex::typeid)
{
#ifdef HANDLE
	_phZeroInflationIndex = NULL;
#endif
	_ppZeroInflationIndex = &childNative;
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
}
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (QuantLib::ZeroInflationIndex& childNative, Object^ owner) : CInflationIndex(CZeroInflationIndex::typeid)
{
#ifdef HANDLE
	_phZeroInflationIndex = NULL;
#endif
	_ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (&childNative);
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
    _ZeroInflationIndexOwner = owner;
    _InflationIndexOwner = owner;
}

Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (CZeroInflationIndex^ copy) : CInflationIndex(CZeroInflationIndex::typeid)
{
#ifdef HANDLE
	_phZeroInflationIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (copy->GetShared());
        _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
    }
}
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (PLATFORM::Type^ t) : CInflationIndex(CZeroInflationIndex::typeid)
{
#ifdef HANDLE
	_phZeroInflationIndex = NULL;
#endif
	if (!t->IsSubclassOf(CZeroInflationIndex::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (QuantLib::Handle<QuantLib::ZeroInflationIndex>& childNative, Object^ owner)  : CInflationIndex(CZeroInflationIndex::typeid)
{
	_phZeroInflationIndex = &childNative;
	_ppZeroInflationIndex = &static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>>(childNative.currentLink());
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
    _ZeroInflationIndexOwner = owner;
}
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (QuantLib::Handle<QuantLib::ZeroInflationIndex> childNative)  : CInflationIndex(CZeroInflationIndex::typeid)
{
	_phZeroInflationIndex = &childNative;
	_ppZeroInflationIndex = &static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>>(childNative.currentLink());
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::CZeroInflationIndex::CZeroInflationIndex (QuantLib::ZeroInflationIndex childNative)  : CInflationIndex(CZeroInflationIndex::typeid)
{
#ifdef HANDLE
	_phZeroInflationIndex = NULL;
#endif
	_ppZeroInflationIndex = new boost::shared_ptr<QuantLib::ZeroInflationIndex> (new QuantLib::ZeroInflationIndex (childNative));
    _ppInflationIndex = new boost::shared_ptr<QuantLib::InflationIndex> (boost::dynamic_pointer_cast<QuantLib::InflationIndex> (*_ppZeroInflationIndex));
}
#endif

Cephei::QL::Indexes::CZeroInflationIndex::~CZeroInflationIndex ()
{
    if (_ppZeroInflationIndex != NULL)
    {
	    delete _ppZeroInflationIndex;
        _ppZeroInflationIndex = NULL;
    }
}
Cephei::QL::Indexes::CZeroInflationIndex::!CZeroInflationIndex ()
{
    if (_ppZeroInflationIndex != NULL)
    {
	    delete _ppZeroInflationIndex;
    }
}
QuantLib::ZeroInflationIndex& Cephei::QL::Indexes::CZeroInflationIndex::GetReference ()
{
    if (_ppZeroInflationIndex == NULL) throw REFNEW NativeNullException ();
	return **_ppZeroInflationIndex;
}
boost::shared_ptr<QuantLib::ZeroInflationIndex>& Cephei::QL::Indexes::CZeroInflationIndex::GetShared ()
{
    if (_ppZeroInflationIndex == NULL) throw REFNEW NativeNullException ();
	return *_ppZeroInflationIndex;
}
QuantLib::ZeroInflationIndex* Cephei::QL::Indexes::CZeroInflationIndex::GetPointer ()
{
    if (_ppZeroInflationIndex == NULL) throw REFNEW NativeNullException ();
	return &**_ppZeroInflationIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ZeroInflationIndex>& Cephei::QL::Indexes::CZeroInflationIndex::GetHandle ()
{
	if (_phZeroInflationIndex == NULL)
	{
		_phZeroInflationIndex = new Handle<QuantLib::ZeroInflationIndex> (*_ppZeroInflationIndex);
	}
	return *_phZeroInflationIndex;
}
#endif
bool Cephei::QL::Indexes::CZeroInflationIndex::HasNative () 
{
	return (_ppZeroInflationIndex != NULL);
}

Double Cephei::QL::Indexes::CZeroInflationIndex::Fixing (DateTime fixingDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ forecastTodaysFixing)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate); //a
        bool _forecastTodaysFixing = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (forecastTodaysFixing) ? (bool)ValueHelper::Convert (forecastTodaysFixing->Value) : false); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppZeroInflationIndex)->fixing ( _fixingDate,  _forecastTodaysFixing );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IZeroInflationTermStructure^ Cephei::QL::Indexes::CZeroInflationIndex::ZeroInflationTermStructure::get ()
{
    try
    {
    	Handle<QuantLib::ZeroInflationTermStructure> _rv = (Handle<QuantLib::ZeroInflationTermStructure>)(*_ppZeroInflationIndex)->zeroInflationTermStructure ( );   
        Cephei::QL::Termstructures::CZeroInflationTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CZeroInflationTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IZeroInflationIndex^ Cephei::QL::Indexes::CZeroInflationIndex::Clone (Cephei::QL::Termstructures::IZeroInflationTermStructure^ h)
{
    CZeroInflationTermStructure^ _Ch;
    try
    {
        _Ch = safe_cast<CZeroInflationTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::ZeroInflationTermStructure>& _h = static_cast<Handle<QuantLib::ZeroInflationTermStructure>&> (_Ch->GetHandle ()); 
    	boost::shared_ptr<QuantLib::ZeroInflationIndex> _rv = (boost::shared_ptr<QuantLib::ZeroInflationIndex>)(*_ppZeroInflationIndex)->clone ( _h );   
        Cephei::QL::Indexes::CZeroInflationIndex^ _nrv = REFNEW Cephei::QL::Indexes::CZeroInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::IZeroInflationIndex^ Cephei::QL::Indexes::CZeroInflationIndex_Factory::Create (String^ familyName, Cephei::QL::Indexes::IRegion^ region, Boolean revised, Boolean interpolated, QL::Times::FrequencyEnum frequency, Cephei::QL::Times::IPeriod^ availabilityLag, Cephei::QL::ICurrency^ currency, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IZeroInflationTermStructure^>^ ts)
{
    return REFNEW CZeroInflationIndex ( familyName,  region,  revised,  interpolated,  frequency,  availabilityLag,  currency,  ts);
}
